òɾۿûѧϰʹá
ԭַhttps://www.joinquant.com/post/11188

ԭһ˵ʽ鵽ԭĺ߽ۡ


ԭĲԴ£

import jqdata

def initialize(context):
    g.t=0
    g.buy_init = 0      #׶μֱg.n2Ϊֹ
    g.sell_init = 0     #׶μֱg.n2Ϊֹ
    g.buy_count = 0     #2ֱﵽg.n3Ϊֹ
    g.sell_count = 0    #2ֱﵽg.n3Ϊֹ
    g.state = 'empty'   #λ״ֻ̬ǿղ
    g.save_price = 0    #¼ϴαļ۸״̬
    g.n1 = 4            #׶κʹǰļ۸бȽ
    g.n2 = 4            #׶Ϊ4
    g.count_lag = 2     #׶κʹǰڶļ۸бȽ
    g.n3 = 4            #׶ٽֵΪ4
    g.line=0.0          
    g.ud = -1
    set_commission(PerTrade(buy_cost=0.002, sell_cost=0.002, min_cost=5))
    set_option('use_real_price', True)
    g.stock = '000300.XSHG' #300ָΪ׶
    set_benchmark('000300.XSHG')
    
def handle_data(context, data):
    g.t+=1
    #ȡź
    signal = get_signal(context)
    #ݽź
    rebalance(signal, context)
    record(line=g.line)
    record(price=attribute_history(g.stock, 1, '1d', ['close']).iloc[0,0])

#ȡźţж뻹
def get_signal(context):
    
    #ǰλΪգѰҵ
    if g.state == 'empty' or g.state == 'buy_count':
        #̼
        prices = list(attribute_history(g.stock, g.n1+g.n2, '1d', 'close').close)
        #ͼ
        prices_low=list(attribute_history(g.stock, g.n1+g.n2, '1d', 'low').low)
        #̼ۼ4ǰͣļӣ
        g.buy_init=0
        for i in range(g.n2):
            if prices[i+g.n1]<=prices[i]:
                g.buy_init = g.buy_init+1
        print ('g.buy_init',g.buy_init)
        #ļﵽn2󣬽ļ
        #̼Ϊһļ۸
        #һͼۣΪֹ
        #
        if g.buy_init == g.n2:
            g.state = 'buy_count'
            g.buy_init = 0
            g.save_price = prices[-1]
            g.low=prices_low[-1]
            g.buy_count = 0
            
    #
    if g.state == 'buy_count':
        prices = attribute_history(g.stock, g.count_lag+1, '1d', ['close','high','low'])
        closes = list(prices.close)
        highs = list(prices.high)
        lows = list(prices.low)
        #С֮ǰֵ
        if closes[-1]>=highs[0] and highs[-1]>highs[-2] and closes[-1]>g.save_price:
            g.save_price = closes[-1]
            g.buy_count += 1
            
            #ֹ
            if(g.line>lows[-1]):
                g.line=lows[-1]
           
        print ('buy counting',g.buy_count)
        #ﵽn3 ͷź
        if g.buy_count == g.n3:
            g.state = 'full'
            g.buy_count = 0
            return 'buy'
    
    #ǰλѰҵ
    if g.state == 'full' or g.state == 'sell_count':
        prices = list(attribute_history(g.stock, g.n1+g.n2, '1d', 'close').close)
        prices_low=list(attribute_history(g.stock, g.n1+g.n2, '1d', 'low').low)
        #4֮ǰ̼ۣļ1ϵ͹
        g.sell_init=0
        for i in range(g.n2):
            if prices[i+g.n1]>=prices[i]:
                g.sell_init = g.sell_init+1
        print ('g.sell_init',g.sell_init)
        #ļn2
        if g.sell_init == g.n2:
            g.state = 'sell_count'
            g.sell_init = 0
            g.save_price = prices[-1]
            g.sell_count = 0

    if g.state == 'sell_count':
        prices = attribute_history(g.stock, g.count_lag+1, '1d', ['close','high','low'])
        closes = list(prices.close)
        highs = list(prices.high)
        lows = list(prices.low)
        if closes[-1]<=lows[0] and lows[-1]<lows[-2] and closes[-1]<g.save_price:
            g.save_price = closes[-1]
            g.sell_count += 1
            if g.line<highs[-1]:
                g.line=highs[-1]
        print ('sell counting',g.sell_count)
        if g.sell_count == g.n3:
            g.state = 'empty'
            g.sell_count = 0
            return 'sell'
        
#ݻõźŽ͵Ϊ0
def rebalance(signal, context):
    if signal=='buy':
        print 'buy'
        order_target_value(g.stock, context.portfolio.total_value)
    if signal=='sell':
        print 'sell'
        order_target(g.stock, 0)
    '''
    price = attribute_history(g.stock, 1, '1d', ['close']).iloc[0,0]
    if g.state == 'empty' and price>g.line:
        print 'buy'
        order_target_value(g.stock, context.portfolio.total_value)
        if g.state != 'sell_count':
            g.state = 'full'
        g.line=0
    if g.state == 'full' and price<g.line:
        print 'sell'
        order_target(g.stock, 0)
        if g.state != 'buy_count':
            g.state = 'empty'
        g.line=0
    '''